//
//  AppDelegate+AppService.m
//  TimeToCoin
//
//  Created by 1-Mac on 2018/8/1.
//  Copyright © 2018年 QuanLi Technology Limited. All rights reserved.
//

#import "AppDelegate+AppService.h"

@implementation AppDelegate (AppService)

//初始化服务
-(void)initService
{
    //网络状态监听
    [kNotificationCenter addObserver:self
                            selector:@selector(netWorkStateChange:)
                                name:KNotificationNetWorkStateChange
                              object:nil];
}

#pragma mark ————— 网络状态变化 —————
- (void)netWorkStateChange:(NSNotification *)notification
{
    BOOL isNetWork = [notification.object boolValue];
    
    if (isNetWork) {//有网络
        
        if ([CKNetworkHelper isWiFiNetwork]) {  //wifi
            
            [SVProgressHUD showInfoWithStatus:@"已连接WiFi"];
            [SVProgressHUD dismissWithDelay:1];
        }else{  //手机自带网
            
            [SVProgressHUD showInfoWithStatus:@"手机自带网络"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    }else {//登陆失败加载登陆页面控制器
        [SVProgressHUD showInfoWithStatus:@"网络状态不佳"];
        [SVProgressHUD dismissWithDelay:1];
    }
    
}

// 初始化窗口
- (void)initWindow
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = KWhiteColor;
    
    [self.window makeKeyAndVisible];
    
    // TODO: 判断是否第一次启动
    if ([SettingInfoTools isFirstRunning] == YES) {
        
        NSLog(@"第一次启动");
        
        [SettingInfoTools recordFirstRunning];
        //在这里设置引导页
        
    }
    
    //版本号判断 -- 确定跳转首页或是引导页
    [self versionCompare];
}

+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

#pragma mark ————— 网络状态监听 —————
- (void)monitorNetworkStatus{
    
    // 网络状态改变一次, networkStatusWithBlock就会响应一次
    [CKNetworkHelper networkStatusWithBlock:^(CKNetworkStatus networkStatus) {
        
        switch (networkStatus) {
                // 未知网络
            case StatusUnknown:
                NSLog(@"网络环境：未知网络");
                // 无网络
            case StatusNotReachable:
                NSLog(@"网络环境：无网络");
                KPostNotification(KNotificationNetWorkStateChange, @NO);
                break;
                // 手机网络
            case StatusReachableViaWWAN:
                NSLog(@"网络环境：手机自带网络");
                KPostNotification(KNotificationNetWorkStateChange, @YES);
                // 无线网络
            case StatusReachableViaWiFi:
                NSLog(@"网络环境：WiFi");
                KPostNotification(KNotificationNetWorkStateChange, @YES);
                break;
        }
        
    }];
}

#pragma mark - 版本号判断
//版本号判断 -- 确定跳转首页或是引导页
-(void)versionCompare{
    // 2.设置根控制器
    NSString *key = @"CFBundleVersion";
    // 上一次的使用版本（存储在沙盒中的版本号）

    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    // 当前软件的版本号（从Info.plist中获得）
    NSString * currentVersion = [NSBundle mainBundle].infoDictionary[key];
    if (!lastVersion) {
        lastVersion = currentVersion;
        // 将当前的版本号存进沙盒
        [self.window makeKeyAndVisible];
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];

        //进入引导页
        self.FeaturenewVC = [[KNewfeatureViewController alloc]init];
        self.window.rootViewController = self.FeaturenewVC;
    }
    if ([currentVersion isEqualToString:lastVersion]) { // 版本号相同：这次打开和上次打开的是同一个版本
        //进入登陆页
//        self.MenuRootVC = [[KMenuRootViewController alloc]init];
//        self.window.rootViewController = self.MenuRootVC;
        
        
        self.loginVC = [[KLoginViewController alloc]init];
        self.window.rootViewController = self.loginVC;
        NSLog(@"%@",[SettingInfoTools isFirstRunning] ? @"YES":@"NO");
        
    } else { // 这次打开的版本和上一次不一样，显示新特性
        //进入引导页
        self.FeaturenewVC = [[KNewfeatureViewController alloc]init];
        self.window.rootViewController = self.FeaturenewVC;
        // 将当前的版本号存进沙盒
        [self.window makeKeyAndVisible];
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
